package com.controller;

import com.bean.dto.Dto;
import com.bean.entity.Department;
import com.bean.entity.Role;
import com.bean.entity.Staff;
import com.bean.vo.PageAssistant;
import com.bean.vo.RoleVo;
import com.bean.vo.StaffQueryVo;
import com.bean.vo.StaffVo;
import com.service.DepartmentService;
import com.service.RoleService;
import com.service.StaffService;
import com.util.DtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 员工业务控制
 */
@RestController
@RequestMapping("s")
public class StaffController {
    /*员工业务服务对象*/
    @Autowired
   private StaffService staffService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private DepartmentService departmentService;

    /**
     * 员工登录
     */
    @RequestMapping("login")
    public Dto login(Staff staff, HttpSession session){

        System.out.println("登录");
        //打印参数
        System.out.println("staff:"+staff);
        //登录员工
        StaffVo loginStaff=null;
        try {
            //查询登录员工
            loginStaff = staffService.query(staff);
            //保存登录员工信息
            session.setAttribute("loginStaff",loginStaff);


        } catch (Exception e) {
            e.printStackTrace();
            //返回登录错误信息
            return DtoUtil.returnFail("账号或密码错误!");
        }
        /*判断员工职务是否是普通职员,普通职员无登录权限*/
        if(loginStaff.getRoleId()==3)
            return DtoUtil.returnFail("该账号无权限登录!");

        //返回登录员工
        return  DtoUtil.returnSuccess(loginStaff);

    }

    /**
     * 退出登录
     */
    @RequestMapping("logoff")
    public Dto logoff(HttpSession session){

        //关闭会话
        session.invalidate();

        return DtoUtil.returnSuccess();

    }

    /**
     * 获取登录员工信息
     * @param session
     * @return
     */
    @RequestMapping("loginStaff")
    public Dto getLoginStaff(HttpSession session){
        //获取登录员工信息
        System.out.println("获取登录员工信息");
        //获取登录员工信息
        StaffVo loginStaff = (StaffVo)session.getAttribute("loginStaff");

        return DtoUtil.returnSuccess(loginStaff);
    }

    /**
     * 获取所有职务信息
     */
    @RequestMapping("getRole")
    public Dto getRole(){

        System.out.println("获取所有职务信息");
        /*查询所有职务*/
        List<RoleVo> roleVoList = roleService.queryAll(new Role());

        System.out.println("roleVoList:"+roleVoList);

        return DtoUtil.returnSuccess(roleVoList);
    }

    /**
     * 获取所有部门信息
     */
    @RequestMapping("getDept")
    public Dto getDept(){
        System.out.println("获取所有部门信息");
        /*查询所有职务*/
        List<Department> deptList = departmentService.queryAll(new Department());

        System.out.println("deptList:"+deptList);
        return DtoUtil.returnSuccess(deptList);
    }


    /**
     * 分页查询员工
     */
    @RequestMapping("queryByPage")
    public Dto queryByPage(PageAssistant<StaffVo> pa, StaffQueryVo staffQueryVo, HttpSession session){
        System.out.println("分页查询员工");
        /*打印参数*/
        System.out.println("pa:"+pa);
        System.out.println("staffQueryVo:"+staffQueryVo);

        //获取登录员工信息
        StaffVo loginStaff = (StaffVo)session.getAttribute("loginStaff");
        //打印参数
        System.out.println("loginStaff:"+loginStaff);
        //获取登录员工的职务级别
        Integer level = loginStaff.getRoleVo().getLevel();
        //获取登录员工的部门
        Integer deptId = loginStaff.getDeptId();
        //在查询vo类中添加职务级别查询条件
        staffQueryVo.setRoleLevel(level);
        if(level==10 || deptId==5){
            //不设职务级别查询条件
            staffQueryVo.setRoleLevel(null);
        }

        //在查询vo类中添加部门查询条件
        staffQueryVo.setDeptId(deptId);
        /*判断职务级别,总经理和人事部员工,可查询所有部门员工信息,其它部门只能查询本部门员工 */
        if(level==10 || deptId==5){
            //不设部门查询条件
            staffQueryVo.setDeptId(null);
        }


        //查询员工信息
        pa = staffService.queryStaffByPage(pa, staffQueryVo);
        /*判断是否查询到员工信息*/
        if(pa.getItems()==null)
            return DtoUtil.returnFail("没有查询到员工信息!");

        System.out.println("pa:"+pa);
        //返回数据
        return DtoUtil.returnSuccess(pa);
    }


    /**
     * 添加员工
     */
    @RequestMapping("save")
    public Dto saveStaff(Staff staff){
        System.out.println("添加员工");
        /*打印参数*/
        System.out.println("staff:"+staff);
        //员工状态
        staff.setState(1);
        //添加员工
        int add = staffService.add(staff);
        //判断是否添加成功
        if (add == 1)
            return DtoUtil.returnSuccess("操作成功!");
        //返回失败提示信息
        return DtoUtil.returnFail("操作失败!");

    }

    /**
     * 员工离职
     */
    @RequestMapping("remove")
    public Dto removeStaff(Staff staff){

        System.out.println("删除员工");
//        修改员工状态
        staff.setState(2);
        int modify = staffService.modify(staff);
        //判断是否删除成功
        if (modify == 1)
            return DtoUtil.returnSuccess("操作成功!");
        //返回失败提示信息
        return DtoUtil.returnFail("操作失败!");
    }

    /**
     * 查询单个员工
     */
    @RequestMapping("unique")
    public Dto uniqueQuery(Staff staff){
        System.out.println("查看单个员工详情");

        //打印参数
        System.out.println("id:"+staff.getId());
        System.out.println("realName:"+staff.getRealName());
        //员工
        StaffVo staffVo =null;
        try {
            //查询员工信息
            staffVo = staffService.query(staff);
        } catch (Exception e) {
            e.printStackTrace();
            //返回查询错误信息
            return DtoUtil.returnFail("没有数据!");
        }
        //返回员工数据
        return DtoUtil.returnSuccess(staffVo);
    }

    /**
     * 修改员工信息
     */
    @RequestMapping("modify")
    public Dto modifyStaff(Staff staff, HttpSession session){
        System.out.println("修改员工信息");

        //获取登录用户
        StaffVo loginStaff = (StaffVo)session.getAttribute("loginStaff");
        //旧用户名
        String userName = loginStaff.getUserName();

        //新的用户名
        String newUserName=staff.getUserName();

        if(!userName.equals(newUserName)){
            StaffVo staffVo =null;

            /*判断账号是否为空*/
            if(staff.getUserName()!=null || !staff.getUserName().equals("")){
                try {
                    //查询用户名是否存在
                    staffVo = staffService.query(new Staff(newUserName));
                    return DtoUtil.returnFail("用户名已存在！");
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }

        int modify = staffService.modify(staff);

        if(modify==0){
            //返回查询错误信息
            return DtoUtil.returnFail("员工信息修改失败!");
        }
        return DtoUtil.returnSuccess("员工信息修改成功!");
    }
    /**
     * 修改密码
     */
    @RequestMapping("modifyPassword")
    public Dto modifyPassword(String newPassword, Staff staff){

        System.out.println("修改密码");
        //打印参数
        System.out.println("newPassword:"+newPassword);
        //员工
        StaffVo staffVo =null;
        try {
            //查询员工信息
            staffVo = staffService.query(staff);
            staff.setPassword(newPassword);
            int modify = staffService.modify(staff);
            if(modify==0){
                //返回查询错误信息
                return DtoUtil.returnFail("密码修改失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            //返回查询错误信息
            return DtoUtil.returnFail("旧密码输入错误!");
        }

        return DtoUtil.returnSuccess("密码修改成功!");
    }

}
